package com.chromatic.competition.utils;

import android.util.Log;

import androidx.annotation.NonNull;

import com.chromatic.competition.model.UserAccount;
import com.chromatic.competition.model.UserInformation;
import com.chromatic.competition.inteface.UserCheckService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 用户管理工具类
 */
public class UserManager {
    private static final String BASE_URL = "https://api.fzuenactus.org.cn/";
    private static UserCheckService userService;

    public UserManager() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(new LenientJsonConverterFactory())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        userService = retrofit.create(UserCheckService.class);
    }

    /**
     * 获取用户个人资料
     *
     * @param userId   用户ID
     * @param callback 回调接口，用于处理请求结果
     */
    public static void getUserInformation(int userId, final UserInformationCallback callback) {
        UserManager manager = new UserManager();
        Call<UserInformation> call = userService.getUserInformation(userId);
        call.enqueue(new Callback<>() {
            @Override
            public void onResponse(@NonNull Call<UserInformation> call, @NonNull Response<UserInformation> response) {
                if (response.isSuccessful()) {
                    UserInformation userInformation = response.body();
                    if (userInformation != null) {
                        callback.onSuccess(userInformation);
//                        Log.d("UserInformationManager的getUserInformation方法","这是获取到的用户个人资料的学号：" + userInformation.getStudentId());
//                        Log.d("UserInformationManager的getUserInformation方法","这是获取到的用户个人资料的姓名：" + userInformation.getRealname());
                    } else {
                        callback.onError("User information is empty");
                    }
                } else {
                    callback.onError("Request failed with code: " + response.code());
                }
            }

            @Override
            public void onFailure(@NonNull Call<UserInformation> call, @NonNull Throwable t) {
                callback.onError("Network request failed: " + t.getMessage());
            }
        });
    }

    /**
     * 更新用户个人资料
     *
     * @param userId          用户ID
     * @param userInformation 用户个人资料
     * @param callback        回调接口，用于处理请求结果
     */
    public void updateUserInformation(int userId, UserInformation userInformation, final UserInformationCallback callback) {
        Call<UserInformation> call = userService.updateUserInformation(userId, userInformation);
        call.enqueue(new Callback<>() {
            @Override
            public void onResponse(@NonNull Call<UserInformation> call, @NonNull Response<UserInformation> response) {
                if (response.isSuccessful()) {
                    UserInformation updatedUserInformation = response.body();
                    if (updatedUserInformation != null) {
                        callback.onSuccess(updatedUserInformation);
                    } else {
                        callback.onError("Updated user information is empty");
                    }
                } else {
                    callback.onError("Request failed with code: " + response.code());
                }
            }

            @Override
            public void onFailure(@NonNull Call<UserInformation> call, @NonNull Throwable t) {
                callback.onError("Network request failed: " + t.getMessage());
            }
        });
    }

    /**
     * 用户个人资料回调接口
     */
    public interface UserInformationCallback {
        void onSuccess(UserInformation userInformation);

        void onError(String errorMessage);
    }

    /**
     * 获取用户账户信息
     *
     * @param userId   用户ID
     * @param callback 回调接口，用于处理请求结果
     */
    public void getUserAccount(int userId, final UserAccountCallback callback) {
        Call<UserAccount> call = userService.getUserAccount(userId);
        call.enqueue(new Callback<>() {
            @Override
            public void onResponse(@NonNull Call<UserAccount> call, @NonNull Response<UserAccount> response) {
                if (response.isSuccessful()) {
                    UserAccount userAccount = response.body();
                    if (userAccount != null) {
                        callback.onSuccess(userAccount);
//                        Log.d("UserAccountManager的getUserAccount方法","这是获取到的用户账户的用户ID：" + userAccount.getUserId());
//                        Log.d("UserAccountManager的getUserAccount方法","这是获取到的用户账户的用户名：" + userAccount.getUsername());
                    } else {
                        callback.onError("User account is empty");
//                        Log.d("UserAccountManager的getUserAccount方法","没有获取到用户账户信息！");
                    }
                } else {
                    callback.onError("Request failed with code: " + response.code());
//                    Log.d("UserAccountManager的getUserAccount方法","请求用户账户信息失败！");
                }
            }

            @Override
            public void onFailure(@NonNull Call<UserAccount> call, @NonNull Throwable t) {
                callback.onError("Network request failed: " + t.getMessage());
//                Log.d("UserAccountManager的getUserAccount方法","网络请求失败！");
            }
        });
    }

    /**
     * 更新用户账户信息
     *
     * @param userId             用户ID
     * @param updatedUserAccount 更新后的用户账户信息
     * @param callback           回调接口，用于处理请求结果
     */
    public void updateUserAccount(int userId, UserAccount updatedUserAccount, final UserAccountCallback callback) {
        Call<UserAccount> call = userService.updateUserAccount(userId, updatedUserAccount);
        call.enqueue(new Callback<>() {
            @Override
            public void onResponse(@NonNull Call<UserAccount> call, @NonNull Response<UserAccount> response) {
                if (response.isSuccessful()) {
                    UserAccount userAccount = response.body();
                    if (userAccount != null) {
                        callback.onSuccess(userAccount);
//                        Log.d("UserAccountManager的updateUserAccount方法","用户账户更新成功!");
                    } else {
                        callback.onError("User account is empty");
                    }
                } else {
                    callback.onError("Request failed with code: " + response.code());
                }
            }

            @Override
            public void onFailure(@NonNull Call<UserAccount> call, @NonNull Throwable t) {
                callback.onError("Network request failed: " + t.getMessage());
            }
        });
    }

    /**
     * 用户账户回调接口
     */
    public interface UserAccountCallback {
        void onSuccess(UserAccount userAccount);

        void onError(String errorMessage);
    }

    /**
     * 获取用户昵称
     *
     * @param userId   用户ID
     * @param callback 回调接口，用于处理请求结果
     */
    public void getUserNickname(int userId, final UserNicknameCallback callback) {
        Call<String> call = userService.getUserNickname(userId);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
                if (response.isSuccessful()) {
                    String nickname = response.body();
//                    Log.d("UserManager的getUserNickname方法","这是昵称的响应体：" + nickname);
                    if (nickname != null) {
                        callback.onSuccess(nickname);
//                        Log.d("UserManager的getUserNickname方法","这是获取到的好友昵称：" + nickname);
                    } else {
                        callback.onError("Nickname is empty");
                    }
                } else {
                    callback.onError("Request failed with code: " + response.code());
                }
            }

            @Override
            public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
                callback.onError("网络请求失败: " + t.getMessage());
            }
        });
    }

    /**
     * 用户昵称回调接口
     */
    public interface UserNicknameCallback {
        void onSuccess(String userNickname);
        void onError(String errorMessage);
    }
}